<?php
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');

if($edit) {    
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.jeditable.js'));
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.charcounter.js'));    
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.jeditable.charcounter.js'));
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.colorbox.js'));
    $cs->registerCssFile(SystemAsset::toUrl('colorbox.css'));
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.colorpicker.js'));
    $cs->registerCssFile(SystemAsset::toUrl('colorpicker.css'));

    $savingImage=CHtml::normalizeUrl(SystemAsset::toUrl('ajax-loader.gif'));
    $indicator=CJavaScript::quote(CHtml::image($savingImage));

    $titleMaxLen=Kitchen::TITLE_MAX_LEN;
    $setTitleUrl=CJavaScript::quote(CHtml::normalizeUrl(array('setTitle','id'=>$kitchen->id)));
    $cs->registerScript('setTitle', 
        "$('#kitchen-title').editable('$setTitleUrl', {
            indicator : '$indicator',
            type      : 'text_charcounter',
            charcounter : {
                characters : $titleMaxLen
            }            
        });"
    );
        
    $setImageUrl=CJavaScript::quote(CHtml::normalizeUrl(array('setImage','id'=>$kitchen->id)));
    $cs->registerScript('setImage', 
        "$('#kitchen-welcomeMessageWrapper').after('');
         $('#edit-image').colorbox({href:'$setImageUrl',width:500, height:240, iframe:true});"
    );
    
    $welcomeMessageMaxLen=Kitchen::WELCOME_MESSAGE_MAX_LEN;
    $setWelcomeMessageUrl=CHtml::normalizeUrl(array('setWelcomeMessage','id'=>$kitchen->id));
    $cs->registerScript('setWelcomeMessage', 
        "$('#kitchen-welcomeMessage').editable('$setWelcomeMessageUrl', {
            indicator : '$indicator',
            type      : 'textarea_charcounter',
            rows      : 5,
            charcounter : {
                characters : $welcomeMessageMaxLen
            }            
        });"
    );

    $descriptionMaxLen=Kitchen::DESCRIPTION_MAX_LEN;
    $setDescriptionUrl=CHtml::normalizeUrl(array('setDescription','id'=>$kitchen->id));
    $cs->registerScript('setDescription', 
        "$('#kitchen-description').editable('$setDescriptionUrl', {
            indicator : '$indicator',
            type      : 'textarea_charcounter',
            rows      : 5,
            charcounter : {
                characters : $descriptionMaxLen
            }            
        });"
    );    
}
?>

<div>
  <h2>
    <div id='kitchen-title'><?php echo CHtml::encode($kitchen->title);?></div>
  </h2>

    <div id='kitchen-welcomeMessageWrapper'>
        <div id='kitchen-welcomeMessage' style='font-weight:800'><?=nl2br(CHtml::encode($kitchen->welcomeMessage));?></div>
    </div>
    
    <script type="text/javascript">
        function setKitchenImage(image)
        {
            $('#kitchen-welcomeMessageWrapper').css('backgroundImage','url("'+image+'")');
        }

        function setKitchenWelcomeMessageColor(color)
        {
            $('#kitchen-welcomeMessageWrapper').css('color','#'+color);
        }
        
        var welcomeMessageWrapper=$('#kitchen-welcomeMessageWrapper');
        welcomeMessageWrapper.width(440);
        welcomeMessageWrapper.height(150);
        welcomeMessageWrapper.css('marginBottom',"5px");
        setKitchenImage("<?=UserImage::toUrl('kitchen',$kitchen->image)?>");
        setKitchenWelcomeMessageColor("<?=$kitchen->welcomeMessageColor?>");
    </script>
    

<?php if($edit) { ?>        
    <div>
        <a id='edit-image' href='#'>修改封面图片</a> 
        <a id='edit-welcome-message-color' href='#'>修改文字颜色</a>
        <a id='save-welcome-message-color' href='#' style='visibility:hidden'>保存文字颜色</a>
    </div>
    
    <script>
        submitWelcomeMessageColor.originalColor='<?=$kitchen->welcomeMessageColor?>';
        submitWelcomeMessageColor.changeToColor='<?=$kitchen->welcomeMessageColor?>';

        function submitWelcomeMessageColor()
        {
            if(submitWelcomeMessageColor.originalColor==submitWelcomeMessageColor.changeToColor){
                return;
            }

            $.post('<?=CJavaScript::quote(CHtml::normalizeUrl(array('setWelcomeMessageColor','id'=>$kitchen->id)));?>',
                {value : submitWelcomeMessageColor.changeToColor},
                function(data){
                    setKitchenWelcomeMessageColor(data.color);
                    submitWelcomeMessageColor.originalColor=data.color;
                    if(!data.succ) {
                        if(data.error) alert(data.error);
                        location.reload();
                    }
                }, 
                'json');
        }
    </script>
<?php 
    $cs->registerScript('setWelcomeMessageColorPicker', 
        "$('#edit-welcome-message-color').ColorPicker({
            onShow: function (colpkr) {
                $(colpkr).fadeIn(500);
                $('#save-welcome-message-color').css('visibility','inherit');
                return false;
            },
            onHide: function (colpkr) {
                $(colpkr).fadeOut(500);
                $('#save-welcome-message-color').css('visibility','hidden');
                submitWelcomeMessageColor();
                return false;
            },
            onChange: function (hsb, hex, rgb) {
                setKitchenWelcomeMessageColor(hex);
                submitWelcomeMessageColor.changeToColor=hex;
            },
            onBeforeShow: function () {
                $(this).ColorPickerSetColor(submitWelcomeMessageColor.originalColor);
            }            
        });"
    );
} 
?>            
    <div>
      <h3>自我介绍</h3>
      <div id='kitchen-description'><?=nl2br(CHtml::encode($kitchen->description))?></div>
    </div>
      
    <br />      

    <div>
      <h3>最新发布的食谱</h3>
    <?php foreach($recipes as $recipe){ ?>
        <div>
          <?php echo CHtml::link(CHtml::encode($recipe->title),array('/recipe/show','id'=>$recipe->id));?>  <?echo date('Y-m-d H:i:s', $recipe->updateTime)?>更新
        </div>
    <?php } ?>
    <p></p>
      <div>
          <?php echo CHtml::link("查看全部({$kitchen->publishedRecipeNum})",array('/recipe/list','id'=>$user->id)); ?>
          <?php if($edit) echo CHtml::link('创建食谱',array('/recipe/create')); ?>
      </div>      
    </div>
    
    <br />

    <div>
      <h3>最新发布的报告</h3>    
      <?php echo /*CHtml::link(*/"查看全部({$kitchen->reportNum})"/*,array('/report/list','id'=>$user->id))*/; ?>
    </div>    

    <br />

    <div>
      <h3>最新发布的评论</h3>    
      <?php echo /*CHtml::link(*/"查看全部({$kitchen->commentNum})"/*,array('/comment/list','id'=>$user->id))*/; ?>
    </div>    

    <br />

    
    <div>
      <h3>最新发布的日记</h3>    
      <?php echo /*CHtml::link(*/"查看全部({$kitchen->publishedDiaryNum})"/*,array('/diary/list','id'=>$user->id))*/; ?>
      <?php if($edit) echo /*CHtml::link(*/'写日记'/*,array('/diary/create'))*/; ?>
    </div>    
</div>